using System;
using System.Collections.Generic;
using System.Threading;
using View.Common;
namespace View.Replay.Application
{
	public class ParameterCollection
	{
		private DateTime lastModifidTime = DateTime.Now;
		private string defGrpName;
		private Dictionary<string, Dictionary<string, PersistentObject>> objGrpDict = new Dictionary<string, Dictionary<string, PersistentObject>>();
		public List<string> NeedSetValueFromServer = new List<string>();
		public bool IsModified
		{
			get;
			set;
		}
		private string fileName
		{
			get;
			set;
		}
		public ParameterCollection(string fileName, string defGrp, bool isLoad)
		{
			this.fileName = fileName;
			this.defGrpName = defGrp;
			if (isLoad)
			{
				this.Load();
			}
		}
		public bool Load()
		{
			string fileName = this.fileName;
			IniFile iniFile = new IniFile(fileName);
			bool result;
			if (!iniFile.Load())
			{
				result = false;
			}
			else
			{
				foreach (string current in iniFile.GetAllSectionName())
				{
					if (!this.objGrpDict.ContainsKey(current))
					{
						this.objGrpDict[current] = new Dictionary<string, PersistentObject>();
					}
					foreach (string current2 in iniFile.GetSectionItemName(current))
					{
						PersistentObject persistentObject = this.GetObject(current, current2.ToUpper());
						if (persistentObject == null)
						{
							persistentObject = new PersistentObject();
						}
						if (persistentObject.Value.VarType != VarType.Undefined)
						{
							persistentObject.Value = Variable.Parse(iniFile.GetString(current, current2), persistentObject.Value.VarType);
						}
						else
						{
							persistentObject.Value = Variable.Parse(iniFile.GetString(current, current2));
						}
						persistentObject.IsSave = true;
						this.objGrpDict[current][current2] = persistentObject;
					}
				}
				if (!this.objGrpDict.ContainsKey(this.defGrpName))
				{
					this.objGrpDict[this.defGrpName] = new Dictionary<string, PersistentObject>();
				}
				this.IsModified = false;
				result = true;
			}
			return result;
		}
        //public bool Save()
        //{
        //    //return CSMApplication.App.AppType != ApplicationType.ReplayApp && this.SaveDirect();
        //}
		public bool SaveDirect()
		{
			string fileName = this.fileName;
			IniFile iniFile = new IniFile(fileName);
			foreach (string current in this.objGrpDict.Keys)
			{
				foreach (KeyValuePair<string, PersistentObject> current2 in this.objGrpDict[current])
				{
					if (current2.Value.IsSave)
					{
						iniFile.WriteString(current, current2.Key, current2.Value.Value.ToString());
					}
				}
			}
			iniFile.Save();
			this.IsModified = false;
			return true;
		}
		public int GetIntValue(string key, int defVal)
		{
			return this.GetIntValue(this.defGrpName, key, defVal);
		}
		public int GetIntValue(string key)
		{
			if (this.GetObject(this.defGrpName, key) == null)
			{
				throw new Exception(key + " not Exist!");
			}
			return this.GetIntValue(this.defGrpName, key, 0);
		}
		public int GetIntValue(string grpName, string key)
		{
			if (this.GetObject(grpName, key) == null)
			{
				throw new Exception(key + " not Exist!");
			}
			Variable objectValue = this.GetObjectValue(grpName, key);
			int result;
			if (objectValue == Variable.Invalid)
			{
				result = 0;
			}
			else
			{
				result = objectValue;
			}
			return result;
		}
        //public int GetIntValue(CsmRegistryKey key, int defVal)
        //{
        //    return this.GetIntValue("ApplicationRegistry", key.ToString(), defVal);
        //}
		public bool GetBoolValue(string key)
		{
			if (this.GetObject(this.defGrpName, key) == null)
			{
				throw new Exception(key + " not Exist!");
			}
			return this.GetBoolValue(this.defGrpName, key, false);
		}
		public bool GetBoolValue(string grpName, string key)
		{
			if (this.GetObject(grpName, key) == null)
			{
				throw new Exception(key + " not Exist!");
			}
			return this.GetBoolValue(grpName, key, false);
		}
        //public bool GetBoolValue(CsmRegistryKey key)
        //{
        //    return this.GetBoolValue("ApplicationRegistry", key.ToString(), false);
        //}
		public string GetStringValue(string key)
		{
			if (this.GetObject(this.defGrpName, key) == null)
			{
				throw new Exception(key + " not Exist!");
			}
			return this.GetStringValue(this.defGrpName, key);
		}
        //public string GetStringValue(CsmRegistryKey key)
        //{
        //    return this.GetStringValue("ApplicationRegistry", key.ToString());
        //}
		public int GetIntValue(string grpName, string key, int defVal)
		{
			Variable objectValue = this.GetObjectValue(grpName, key);
			int result;
			if (objectValue == Variable.Invalid)
			{
				result = defVal;
			}
			else
			{
				result = objectValue;
			}
			return result;
		}
		public bool GetBoolValue(string key, bool defVal)
		{
			return this.GetBoolValue(this.defGrpName, key, defVal);
		}
		public bool GetBoolValue(string grpName, string key, bool defVal)
		{
			Variable objectValue = this.GetObjectValue(grpName, key);
			bool result;
			if (objectValue == Variable.Invalid)
			{
				result = defVal;
			}
			else
			{
				if (objectValue.VarType == VarType.String)
				{
					string text = objectValue;
					if (!string.IsNullOrEmpty(text))
					{
						text = text.ToLower();
					}
					result = ("是".Equals(text) || "true".Equals(text) || "yes".Equals(text) || "ok".Equals(text) || "1".Equals(text));
				}
				else
				{
					if (objectValue.VarType == VarType.Bool)
					{
						result = objectValue;
					}
					else
					{
						if (objectValue.VarType == VarType.Int)
						{
							result = (objectValue != 0);
						}
						else
						{
							result = defVal;
						}
					}
				}
			}
			return result;
		}
		public string GetStringValue(string grpName, string key)
		{
			return this.GetStringValue(grpName, key, "");
		}
		public string GetStringValue(string grpName, string key, string defVal)
		{
			Variable objectValue = this.GetObjectValue(grpName, key);
			string result;
			if (objectValue == Variable.Invalid)
			{
				result = defVal;
			}
			else
			{
				result = objectValue;
			}
			return result;
		}
		public bool IsContainGroup(string grpName)
		{
			Dictionary<string, Dictionary<string, PersistentObject>> obj;
			Monitor.Enter(obj = this.objGrpDict);
			bool result;
			try
			{
				if (this.objGrpDict.ContainsKey(grpName))
				{
					result = true;
				}
				else
				{
					result = false;
				}
			}
			finally
			{
				Monitor.Exit(obj);
			}
			return result;
		}
		public bool IsContainKey(string key)
		{
			return this.IsContainKey(this.defGrpName, key);
		}
		public bool IsContainKey(string grpName, string key)
		{
			Dictionary<string, Dictionary<string, PersistentObject>> obj;
			Monitor.Enter(obj = this.objGrpDict);
			bool result;
			try
			{
				if (this.objGrpDict.ContainsKey(grpName))
				{
					if (this.objGrpDict[grpName].ContainsKey(key))
					{
						result = true;
					}
					else
					{
						result = false;
					}
				}
				else
				{
					result = false;
				}
			}
			finally
			{
				Monitor.Exit(obj);
			}
			return result;
		}
        //public Variable GetObjectValue(CsmRegistryKey key)
        //{
        //    return this.GetObjectValue("ApplicationRegistry", key.ToString());
        //}
		private Variable GetObjectValue(string grpName, string key)
		{
			Variable result = Variable.Invalid;
			Dictionary<string, Dictionary<string, PersistentObject>> obj;
			Monitor.Enter(obj = this.objGrpDict);
			try
			{
				if (this.objGrpDict.ContainsKey(grpName))
				{
					if (this.objGrpDict[grpName].ContainsKey(key))
					{
						result = this.objGrpDict[grpName][key].Value;
					}
				}
			}
			finally
			{
				Monitor.Exit(obj);
			}
			return result;
		}
        //public object GetValue(CsmRegistryKey key)
        //{
        //    return this.GetValue("ApplicationRegistry", key.ToString());
        //}
		public object GetValue(string objName)
		{
			return this.GetValue(this.defGrpName, objName);
		}
		public object GetValue(string grpName, string objName)
		{
			Variable objectValue = this.GetObjectValue(grpName, objName);
			object result;
			if (objectValue == Variable.Invalid)
			{
				result = null;
			}
			else
			{
				result = objectValue.Value;
			}
			return result;
		}
		public PersistentObject GetObject(string grpName, string objName)
		{
			PersistentObject result;
			if (this.objGrpDict.ContainsKey(grpName))
			{
				if (this.objGrpDict[grpName].ContainsKey(objName))
				{
					result = this.objGrpDict[grpName][objName];
					return result;
				}
			}
			result = null;
			return result;
		}
        //public bool RemoveObject(CsmRegistryKey objName)
        //{
        //    return this.RemoveObject("ApplicationRegistry", objName.ToString());
        //}
		public bool RemoveObject(string objName)
		{
			return this.RemoveObject(this.defGrpName, objName);
		}
		public bool RemoveObject(string grpName, string objName)
		{
			bool result;
			if (string.IsNullOrEmpty(objName) || string.IsNullOrEmpty(grpName))
			{
				result = false;
			}
			else
			{
				bool flag = false;
				Dictionary<string, Dictionary<string, PersistentObject>> obj;
				Monitor.Enter(obj = this.objGrpDict);
				try
				{
					if (this.objGrpDict[grpName].Remove(objName))
					{
						this.IsModified = true;
						this.lastModifidTime = DateTime.Now;
						flag = true;
					}
				}
				finally
				{
					Monitor.Exit(obj);
				}
				result = flag;
			}
			return result;
		}
		public bool RemoveAllObject()
		{
			bool result = false;
			Dictionary<string, Dictionary<string, PersistentObject>> obj;
			Monitor.Enter(obj = this.objGrpDict);
			try
			{
				this.objGrpDict.Clear();
				this.IsModified = true;
				this.lastModifidTime = DateTime.Now;
				result = true;
			}
			finally
			{
				Monitor.Exit(obj);
			}
			return result;
		}
		public ICollection<string> GetGroupList()
		{
			return this.objGrpDict.Keys;
		}
		public ICollection<PersistentObject> GetGroupObjectList(string grpName)
		{
			ICollection<PersistentObject> result;
			if (this.objGrpDict.ContainsKey(grpName))
			{
				result = this.objGrpDict[grpName].Values;
			}
			else
			{
				result = null;
			}
			return result;
		}
		public string GetFileName()
		{
			return this.fileName;
		}
		public bool Add(PersistentObject pObj)
		{
			bool result;
			if (pObj == null)
			{
				result = false;
			}
			else
			{
				Dictionary<string, Dictionary<string, PersistentObject>> obj;
				Monitor.Enter(obj = this.objGrpDict);
				try
				{
					string groupName = pObj.GroupName;
					if (!this.objGrpDict.ContainsKey(groupName))
					{
						this.objGrpDict[groupName] = new Dictionary<string, PersistentObject>();
					}
					this.objGrpDict[groupName][pObj.Name] = pObj;
					this.IsModified = true;
					this.lastModifidTime = DateTime.Now;
				}
				finally
				{
					Monitor.Exit(obj);
				}
				result = true;
			}
			return result;
		}
		public bool Add(string grpName, string objName, object obj)
		{
			return this.Add(grpName, objName, obj, true);
		}
		public bool Add(string grpName, string objName, object obj, bool isSave)
		{
			bool result;
			if (string.IsNullOrEmpty(objName))
			{
				result = false;
			}
			else
			{
				Dictionary<string, Dictionary<string, PersistentObject>> obj2;
				Monitor.Enter(obj2 = this.objGrpDict);
				try
				{
					if (!this.objGrpDict.ContainsKey(grpName))
					{
						this.objGrpDict[grpName] = new Dictionary<string, PersistentObject>();
					}
					PersistentObject persistentObject = new PersistentObject(obj, isSave);
					persistentObject.GroupName = grpName;
					persistentObject.Name = objName;
					persistentObject.Rights = ParameterRight.User;
					this.objGrpDict[grpName][objName] = persistentObject;
					this.IsModified = true;
					this.lastModifidTime = DateTime.Now;
				}
				finally
				{
					Monitor.Exit(obj2);
				}
				result = true;
			}
			return result;
		}
        //public bool Add(CsmRegistryKey key, object obj)
        //{
        //    return this.Add("ApplicationRegistry", key.ToString(), obj, false);
        //}
		public bool Add(string objName, object obj)
		{
			return this.Add(this.defGrpName, objName, obj);
		}
		public void ResetValueFromServer(Dictionary<string, bool> dict)
		{
			foreach (string current in dict.Keys)
			{
				if (this.NeedSetValueFromServer.Contains(current))
				{
					if (this.objGrpDict.ContainsKey("ApplicationRegistry"))
					{
						if (this.objGrpDict["ApplicationRegistry"].ContainsKey(current))
						{
							this.objGrpDict["ApplicationRegistry"][current].Value = dict[current];
						}
					}
				}
			}
		}
	}
}
